home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / pc / files / t_unix / j109lxa4.tar / nr4hdr.c < prev    next >
C/C++ Source or Header  |  1994-06-04  |  4KB  |  151 lines

  1. /* Net/rom transport layer header conversion routines.
  2.  * Copyright 1989 by Daniel M. Frank, W9NK.  Permission granted for
  3.  * non-commercial distribution only.
  4.  */
  5. #include "global.h"
  6. #include "mbuf.h"
  7. #include "nr4.h"
  8.  
  9. /* Convert a net/rom transport header to host format structure.
  10.  * Return -1 if error, 0 if OK.
  11.  */
  12. int
  13. ntohnr4(hdr,bpp)
  14. register struct nr4hdr *hdr;
  15. struct mbuf **bpp;
  16. {
  17.     unsigned char tbuf[NR4MINHDR];
  18.     int i;
  19.  
  20.     if(pullup(bpp, (char *)tbuf, NR4MINHDR) < NR4MINHDR)
  21.         return -1;
  22.  
  23.     hdr->opcode = tbuf[4];
  24.  
  25.     switch(tbuf[4] & NR4OPCODE){
  26.         case NR4OPPID:        /* protocol ID extension */
  27.             hdr->u.pid.family = tbuf[0];
  28.             hdr->u.pid.proto = tbuf[1];
  29.             break;
  30.         case NR4OPCONRQ:    /* connect request */
  31.             hdr->u.conreq.myindex = tbuf[0];
  32.             hdr->u.conreq.myid = tbuf[1];
  33.             if((i = PULLCHAR(bpp)) == -1)
  34.                 return -1;
  35.             hdr->u.conreq.window = i;
  36.             if(pullup(bpp,hdr->u.conreq.user,AXALEN) < AXALEN)
  37.                 return -1;
  38.             if(pullup(bpp,hdr->u.conreq.node,AXALEN) < AXALEN)
  39.                 return -1;
  40.             break;
  41.         case NR4OPCONAK:    /* connect acknowledge */
  42.             hdr->yourindex = tbuf[0];
  43.             hdr->yourid = tbuf[1];
  44.             hdr->u.conack.myindex = tbuf[2];
  45.             hdr->u.conack.myid = tbuf[3];
  46.             if((i = PULLCHAR(bpp)) == -1)
  47.                 return -1;
  48.             hdr->u.conack.window = i;
  49.             break;
  50.         case NR4OPDISRQ:    /* disconnect request */
  51.             hdr->yourindex = tbuf[0];
  52.             hdr->yourid = tbuf[1];
  53.             break;
  54.         case NR4OPDISAK:    /* disconnect acknowledge */
  55.             hdr->yourindex = tbuf[0];
  56.             hdr->yourid = tbuf[1];
  57.             break;
  58.         case NR4OPINFO:        /* information frame */
  59.             hdr->yourindex = tbuf[0];
  60.             hdr->yourid = tbuf[1];
  61.             hdr->u.info.txseq = tbuf[2];
  62.             hdr->u.info.rxseq = tbuf[3];
  63.             break;
  64.         case NR4OPACK:        /* information acknowledge */
  65.             hdr->yourindex = tbuf[0];
  66.             hdr->yourid = tbuf[1];
  67.             /* tbuf[2], ordinarily used for txseq,
  68.                is not significant for info ack */
  69.             hdr->u.ack.rxseq = tbuf[3];
  70.             break;
  71.         default:        /* what kind of frame is this? */
  72.             return -1;
  73.     }
  74.     return 0;
  75. }
  76.  
  77. /* Convert host-format level 4 header to network format */
  78. struct mbuf *
  79. htonnr4(hdr)
  80. register struct nr4hdr *hdr;
  81. {
  82.     static int16 hlen[NR4NUMOPS] = {5,20,6,5,5,5,5};
  83.     struct mbuf *rbuf;
  84.     register char *cp;
  85.     unsigned char opcode;
  86.  
  87.     opcode = hdr->opcode & NR4OPCODE;
  88.  
  89.     if(opcode >= NR4NUMOPS)
  90.         return NULLBUF;
  91.  
  92.     if(hdr == (struct nr4hdr *)NULL)
  93.         return NULLBUF;
  94.  
  95.     if((rbuf = alloc_mbuf(hlen[opcode])) == NULLBUF)
  96.         return NULLBUF;
  97.  
  98.     rbuf->cnt = hlen[opcode];
  99.     cp = rbuf->data;
  100.  
  101.     cp[4] = hdr->opcode;
  102.     
  103.     switch(opcode){
  104.         case NR4OPPID:
  105.             *cp++ = hdr->u.pid.family;
  106.             *cp = hdr->u.pid.proto;
  107.             break;
  108.         case NR4OPCONRQ:
  109.             *cp++ = hdr->u.conreq.myindex;
  110.             *cp++ = hdr->u.conreq.myid;
  111.             cp += 3; /* skip to sixth byte */
  112.             *cp++ = hdr->u.conreq.window;
  113.             memcpy(cp,hdr->u.conreq.user,AXALEN);
  114.             cp += AXALEN;
  115.             memcpy(cp,hdr->u.conreq.node,AXALEN);
  116.             cp += AXALEN;
  117.             break;
  118.         case NR4OPCONAK:
  119.             *cp++ = hdr->yourindex;
  120.             *cp++ = hdr->yourid;
  121.             *cp++ = hdr->u.conack.myindex;
  122.             *cp++ = hdr->u.conack.myid;
  123.             cp++;    /* already loaded pid */
  124.             *cp = hdr->u.conack.window;
  125.             break;
  126.         case NR4OPDISRQ:
  127.             *cp++ = hdr->yourindex;
  128.             *cp = hdr->yourid;
  129.             break;
  130.         case NR4OPDISAK:
  131.             *cp++ = hdr->yourindex;
  132.             *cp = hdr->yourid;
  133.             break;
  134.         case NR4OPINFO:
  135.             *cp++ = hdr->yourindex;
  136.             *cp++ = hdr->yourid;
  137.             *cp++ = hdr->u.info.txseq;
  138.             *cp = hdr->u.info.rxseq;
  139.             break;
  140.         case NR4OPACK:
  141.             *cp++ = hdr->yourindex;
  142.             *cp++ = hdr->yourid;
  143.             *cp++ = 0;  /* txseq field is don't care, but
  144.                        emulate real netrom and G8BPQ
  145.                        behavior with a zero -- N1BEE */
  146.             *cp = hdr->u.ack.rxseq;
  147.             break;
  148.     }
  149.     return rbuf;
  150. }
  151.